import 'package:flutter/material.dart';
import 'dart:math' as math;

/// 手绘风格的装饰类型
enum HandDrawnDecorationType {
  cloud,
  sun,
  flower,
  leaf,
  star,
  heart,
  circle,
  square,
  triangle,
  wave,
  scribble,
}

/// 手绘风格的装饰组件
class HandDrawnDecoration extends StatelessWidget {
  final HandDrawnDecorationType type;
  final double size;
  final Color color;
  final double rotation;
  final double opacity;
  
  const HandDrawnDecoration({
    Key? key,
    required this.type,
    this.size = 60.0,
    this.color = Colors.black,
    this.rotation = 0.0,
    this.opacity = 1.0,
  }) : super(key: key);
  
  @override
  Widget build(BuildContext context) {
    return Opacity(
      opacity: opacity,
      child: Transform.rotate(
        angle: rotation,
        child: CustomPaint(
          size: Size(size, size),
          painter: _HandDrawnDecorationPainter(
            type: type,
            color: color,
          ),
        ),
      ),
    );
  }
}

/// 手绘风格的背景装饰组件
class HandDrawnBackground extends StatelessWidget {
  final Widget child;
  final int decorationCount;
  final List<HandDrawnDecorationType> decorationTypes;
  final List<Color> decorationColors;
  final double minSize;
  final double maxSize;
  final double minOpacity;
  final double maxOpacity;
  
  const HandDrawnBackground({
    Key? key,
    required this.child,
    this.decorationCount = 10,
    this.decorationTypes = const [
      HandDrawnDecorationType.cloud,
      HandDrawnDecorationType.sun,
      HandDrawnDecorationType.flower,
      HandDrawnDecorationType.leaf,
      HandDrawnDecorationType.star,
      HandDrawnDecorationType.heart,
    ],
    this.decorationColors = const [
      Colors.blue,
      Colors.green,
      Colors.orange,
      Colors.purple,
      Colors.red,
      Colors.teal,
    ],
    this.minSize = 20.0,
    this.maxSize = 60.0,
    this.minOpacity = 0.1,
    this.maxOpacity = 0.3,
  }) : super(key: key);
  
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        // 装饰元素
        ...List.generate(decorationCount, (index) {
          final random = math.Random(index);
          final type = decorationTypes[random.nextInt(decorationTypes.length)];
          final color = decorationColors[random.nextInt(decorationColors.length)];
          final size = minSize + random.nextDouble() * (maxSize - minSize);
          final rotation = random.nextDouble() * math.pi * 2;
          final opacity = minOpacity + random.nextDouble() * (maxOpacity - minOpacity);
          
          return Positioned(
            left: random.nextDouble() * 300,
            top: random.nextDouble() * 600,
            child: HandDrawnDecoration(
              type: type,
              size: size,
              color: color,
              rotation: rotation,
              opacity: opacity,
            ),
          );
        }),
        
        // 主要内容
        child,
      ],
    );
  }
}

/// 手绘风格的装饰绘制器
class _HandDrawnDecorationPainter extends CustomPainter {
  final HandDrawnDecorationType type;
  final Color color;
  
  _HandDrawnDecorationPainter({
    required this.type,
    required this.color,
  });
  
  @override
  void paint(Canvas canvas, Size size) {
    final center = Offset(size.width / 2, size.height / 2);
    final radius = size.width / 2;
    final random = _PseudoRandom(type.index * 1000);
    
    final paint = Paint()
      ..color = color
      ..style = PaintingStyle.stroke
      ..strokeWidth = 2.0
      ..strokeCap = StrokeCap.round
      ..strokeJoin = StrokeJoin.round
      ..isAntiAlias = true;
    
    switch (type) {
      case HandDrawnDecorationType.cloud:
        _drawCloud(canvas, center, radius, paint, random);
        break;
      case HandDrawnDecorationType.sun:
        _drawSun(canvas, center, radius, paint, random);
        break;
      case HandDrawnDecorationType.flower:
        _drawFlower(canvas, center, radius, paint, random);
        break;
      case HandDrawnDecorationType.leaf:
        _drawLeaf(canvas, center, radius, paint, random);
        break;
      case HandDrawnDecorationType.star:
        _drawStar(canvas, center, radius, paint, random);
        break;
      case HandDrawnDecorationType.heart:
        _drawHeart(canvas, center, radius, paint, random);
        break;
      case HandDrawnDecorationType.circle:
        _drawCircle(canvas, center, radius, paint, random);
        break;
      case HandDrawnDecorationType.square:
        _drawSquare(canvas, center, radius, paint, random);
        break;
      case HandDrawnDecorationType.triangle:
        _drawTriangle(canvas, center, radius, paint, random);
        break;
      case HandDrawnDecorationType.wave:
        _drawWave(canvas, center, radius, paint, random);
        break;
      case HandDrawnDecorationType.scribble:
        _drawScribble(canvas, center, radius, paint, random);
        break;
    }
  }
  
  // 绘制云朵
  void _drawCloud(Canvas canvas, Offset center, double radius, Paint paint, _PseudoRandom random) {
    final path = Path();
    final cloudWidth = radius * 1.8;
    final cloudHeight = radius * 1.2;
    
    // 绘制云朵的主体
    final mainCircleRadius = cloudHeight * 0.6;
    final mainCircleCenter = Offset(
      center.dx - cloudWidth * 0.1 + random.nextDouble() * 4 - 2,
      center.dy + random.nextDouble() * 4 - 2,
    );
    
    _drawWobblyCircle(
      canvas, 
      mainCircleCenter, 
      mainCircleRadius, 
      paint, 
      random,
    );
    
    // 绘制左侧的小圆
    final leftCircleRadius = cloudHeight * 0.4;
    final leftCircleCenter = Offset(
      center.dx - cloudWidth * 0.4 + random.nextDouble() * 4 - 2,
      center.dy + cloudHeight * 0.1 + random.nextDouble() * 4 - 2,
    );
    
    _drawWobblyCircle(
      canvas, 
      leftCircleCenter, 
      leftCircleRadius, 
      paint, 
      random,
    );
    
    // 绘制右侧的小圆
    final rightCircleRadius = cloudHeight * 0.5;
    final rightCircleCenter = Offset(
      center.dx + cloudWidth * 0.3 + random.nextDouble() * 4 - 2,
      center.dy - cloudHeight * 0.1 + random.nextDouble() * 4 - 2,
    );
    
    _drawWobblyCircle(
      canvas, 
      rightCircleCenter, 
      rightCircleRadius, 
      paint, 
      random,
    );
  }
  
  // 绘制太阳
  void _drawSun(Canvas canvas, Offset center, double radius, Paint paint, _PseudoRandom random) {
    // 绘制太阳的圆形
    _drawWobblyCircle(canvas, center, radius * 0.6, paint, random);
    
    // 绘制太阳光芒
    final rayCount = 8 + random.nextInt(5);
    final innerRadius = radius * 0.6;
    final outerRadius = radius * 0.9;
    
    for (var i = 0; i < rayCount; i++) {
      final angle = i * 2 * math.pi / rayCount;
      final startX = center.dx + innerRadius * math.cos(angle);
      final startY = center.dy + innerRadius * math.sin(angle);
      final endX = center.dx + outerRadius * math.cos(angle) + random.nextDouble() * 4 - 2;
      final endY = center.dy + outerRadius * math.sin(angle) + random.nextDouble() * 4 - 2;
      
      final path = Path();
      path.moveTo(startX, startY);
      
      // 添加一些波动使线条看起来像手绘的
      final controlX = (startX + endX) / 2 + random.nextDouble() * 4 - 2;
      final controlY = (startY + endY) / 2 + random.nextDouble() * 4 - 2;
      
      path.quadraticBezierTo(controlX, controlY, endX, endY);
      canvas.drawPath(path, paint);
    }
  }
  
  // 绘制花朵
  void _drawFlower(Canvas canvas, Offset center, double radius, Paint paint, _PseudoRandom random) {
    // 绘制花朵的中心
    _drawWobblyCircle(canvas, center, radius * 0.3, paint, random);
    
    // 绘制花瓣
    final petalCount = 5 + random.nextInt(3);
    final innerRadius = radius * 0.3;
    final outerRadius = radius * 0.9;
    
    for (var i = 0; i < petalCount; i++) {
      final angle = i * 2 * math.pi / petalCount;
      final petalCenter = Offset(
        center.dx + (innerRadius + outerRadius) / 2 * math.cos(angle),
        center.dy + (innerRadius + outerRadius) / 2 * math.sin(angle),
      );
      
      final petalPath = Path();
      final petalRadius = radius * 0.3;
      
      // 创建椭圆形花瓣
      for (var j = 0; j <= 20; j++) {
        final t = j / 20;
        final theta = t * math.pi;
        final wobble = 1.0 + (random.nextDouble() * 0.1 - 0.05);
        
        final x = petalCenter.dx + petalRadius * wobble * math.cos(theta) * math.cos(angle) - 
                  petalRadius * 0.5 * wobble * math.sin(theta) * math.sin(angle);
        final y = petalCenter.dy + petalRadius * wobble * math.cos(theta) * math.sin(angle) + 
                  petalRadius * 0.5 * wobble * math.sin(theta) * math.cos(angle);
        
        if (j == 0) {
          petalPath.moveTo(x, y);
        } else {
          petalPath.lineTo(x, y);
        }
      }
      
      canvas.drawPath(petalPath, paint);
    }
  }
  
  // 绘制叶子
  void _drawLeaf(Canvas canvas, Offset center, double radius, Paint paint, _PseudoRandom random) {
    final path = Path();
    final leafLength = radius * 1.6;
    final leafWidth = radius * 0.8;
    
    // 叶子的主干
    final stemPath = Path();
    final stemStartX = center.dx;
    final stemStartY = center.dy + radius * 0.8;
    final stemEndX = center.dx;
    final stemEndY = center.dy - radius * 0.8;
    
    stemPath.moveTo(stemStartX, stemStartY);
    
    // 添加一些波动使线条看起来像手绘的
    final segments = 10;
    for (var i = 1; i <= segments; i++) {
      final t = i / segments;
      final x = stemStartX + (stemEndX - stemStartX) * t;
      final y = stemStartY + (stemEndY - stemStartY) * t;
      final offset = random.nextDouble() * 4 - 2;
      
      stemPath.lineTo(x + offset, y);
    }
    
    canvas.drawPath(stemPath, paint);
    
    // 叶子的轮廓
    final leafPath = Path();
    leafPath.moveTo(center.dx, center.dy - radius * 0.8);
    
    // 左侧曲线
    leafPath.quadraticBezierTo(
      center.dx - leafWidth + random.nextDouble() * 4 - 2,
      center.dy - radius * 0.4 + random.nextDouble() * 4 - 2,
      center.dx - leafWidth * 0.2 + random.nextDouble() * 4 - 2,
      center.dy + random.nextDouble() * 4 - 2,
    );
    
    leafPath.quadraticBezierTo(
      center.dx - leafWidth * 0.1 + random.nextDouble() * 4 - 2,
      center.dy + radius * 0.4 + random.nextDouble() * 4 - 2,
      center.dx + random.nextDouble() * 4 - 2,
      center.dy + radius * 0.8 + random.nextDouble() * 4 - 2,
    );
    
    // 右侧曲线
    leafPath.quadraticBezierTo(
      center.dx + leafWidth * 0.1 + random.nextDouble() * 4 - 2,
      center.dy + radius * 0.4 + random.nextDouble() * 4 - 2,
      center.dx + leafWidth * 0.2 + random.nextDouble() * 4 - 2,
      center.dy + random.nextDouble() * 4 - 2,
    );
    
    leafPath.quadraticBezierTo(
      center.dx + leafWidth + random.nextDouble() * 4 - 2,
      center.dy - radius * 0.4 + random.nextDouble() * 4 - 2,
      center.dx + random.nextDouble() * 4 - 2,
      center.dy - radius * 0.8 + random.nextDouble() * 4 - 2,
    );
    
    canvas.drawPath(leafPath, paint);
    
    // 叶子的脉络
    final veinPath = Path();
    final veinCount = 3 + random.nextInt(3);
    
    for (var i = 0; i < veinCount; i++) {
      final t = (i + 1) / (veinCount + 1);
      final startX = center.dx;
      final startY = center.dy - radius * 0.8 + t * radius * 1.6;
      
      veinPath.moveTo(startX, startY);
      
      // 左侧脉络
      final leftEndX = center.dx - leafWidth * 0.7 * (1 - t);
      final leftEndY = startY - radius * 0.1 + random.nextDouble() * 4 - 2;
      
      veinPath.quadraticBezierTo(
        (startX + leftEndX) / 2 + random.nextDouble() * 4 - 2,
        (startY + leftEndY) / 2 + random.nextDouble() * 4 - 2,
        leftEndX,
        leftEndY,
      );
      
      // 右侧脉络
      veinPath.moveTo(startX, startY);
      
      final rightEndX = center.dx + leafWidth * 0.7 * (1 - t);
      final rightEndY = startY - radius * 0.1 + random.nextDouble() * 4 - 2;
      
      veinPath.quadraticBezierTo(
        (startX + rightEndX) / 2 + random.nextDouble() * 4 - 2,
        (startY + rightEndY) / 2 + random.nextDouble() * 4 - 2,
        rightEndX,
        rightEndY,
      );
    }
    
    canvas.drawPath(veinPath, paint);
  }
  
  // 绘制星星
  void _drawStar(Canvas canvas, Offset center, double radius, Paint paint, _PseudoRandom random) {
    final path = Path();
    final pointCount = 5;
    final innerRadius = radius * 0.4;
    final outerRadius = radius * 0.9;
    
    for (var i = 0; i < pointCount * 2; i++) {
      final angle = i * math.pi / pointCount;
      final r = i % 2 == 0 ? outerRadius : innerRadius;
      final x = center.dx + r * math.cos(angle) + random.nextDouble() * 3 - 1.5;
      final y = center.dy + r * math.sin(angle) + random.nextDouble() * 3 - 1.5;
      
      if (i == 0) {
        path.moveTo(x, y);
      } else {
        path.lineTo(x, y);
      }
    }
    
    path.close();
    canvas.drawPath(path, paint);
  }
  
  // 绘制心形
  void _drawHeart(Canvas canvas, Offset center, double radius, Paint paint, _PseudoRandom random) {
    final path = Path();
    final width = radius * 2;
    final height = radius * 2;
    
    path.moveTo(
      center.dx + random.nextDouble() * 3 - 1.5,
      center.dy + height * 0.3 + random.nextDouble() * 3 - 1.5,
    );
    
    // 左侧曲线
    path.cubicTo(
      center.dx - width * 0.3 + random.nextDouble() * 4 - 2,
      center.dy + height * 0.1 + random.nextDouble() * 4 - 2,
      center.dx - width * 0.5 + random.nextDouble() * 4 - 2,
      center.dy - height * 0.3 + random.nextDouble() * 4 - 2,
      center.dx + random.nextDouble() * 4 - 2,
      center.dy - height * 0.5 + random.nextDouble() * 4 - 2,
    );
    
    // 右侧曲线
    path.cubicTo(
      center.dx + width * 0.5 + random.nextDouble() * 4 - 2,
      center.dy - height * 0.3 + random.nextDouble() * 4 - 2,
      center.dx + width * 0.3 + random.nextDouble() * 4 - 2,
      center.dy + height * 0.1 + random.nextDouble() * 4 - 2,
      center.dx + random.nextDouble() * 3 - 1.5,
      center.dy + height * 0.3 + random.nextDouble() * 3 - 1.5,
    );
    
    canvas.drawPath(path, paint);
  }
  
  // 绘制圆形
  void _drawCircle(Canvas canvas, Offset center, double radius, Paint paint, _PseudoRandom random) {
    _drawWobblyCircle(canvas, center, radius * 0.8, paint, random);
  }
  
  // 绘制正方形
  void _drawSquare(Canvas canvas, Offset center, double radius, Paint paint, _PseudoRandom random) {
    final path = Path();
    final size = radius * 1.5;
    
    final topLeft = Offset(
      center.dx - size / 2 + random.nextDouble() * 4 - 2,
      center.dy - size / 2 + random.nextDouble() * 4 - 2,
    );
    
    final topRight = Offset(
      center.dx + size / 2 + random.nextDouble() * 4 - 2,
      center.dy - size / 2 + random.nextDouble() * 4 - 2,
    );
    
    final bottomRight = Offset(
      center.dx + size / 2 + random.nextDouble() * 4 - 2,
      center.dy + size / 2 + random.nextDouble() * 4 - 2,
    );
    
    final bottomLeft = Offset(
      center.dx - size / 2 + random.nextDouble() * 4 - 2,
      center.dy + size / 2 + random.nextDouble() * 4 - 2,
    );
    
    path.moveTo(topLeft.dx, topLeft.dy);
    
    // 添加一些波动使线条看起来像手绘的
    _addWobblyLine(path, topLeft.dx, topLeft.dy, topRight.dx, topRight.dy, random);
    _addWobblyLine(path, topRight.dx, topRight.dy, bottomRight.dx, bottomRight.dy, random);
    _addWobblyLine(path, bottomRight.dx, bottomRight.dy, bottomLeft.dx, bottomLeft.dy, random);
    _addWobblyLine(path, bottomLeft.dx, bottomLeft.dy, topLeft.dx, topLeft.dy, random);
    
    canvas.drawPath(path, paint);
  }
  
  // 绘制三角形
  void _drawTriangle(Canvas canvas, Offset center, double radius, Paint paint, _PseudoRandom random) {
    final path = Path();
    final size = radius * 1.8;
    
    final top = Offset(
      center.dx + random.nextDouble() * 4 - 2,
      center.dy - size / 2 + random.nextDouble() * 4 - 2,
    );
    
    final bottomRight = Offset(
      center.dx + size / 2 + random.nextDouble() * 4 - 2,
      center.dy + size / 2 + random.nextDouble() * 4 - 2,
    );
    
    final bottomLeft = Offset(
      center.dx - size / 2 + random.nextDouble() * 4 - 2,
      center.dy + size / 2 + random.nextDouble() * 4 - 2,
    );
    
    path.moveTo(top.dx, top.dy);
    
    // 添加一些波动使线条看起来像手绘的
    _addWobblyLine(path, top.dx, top.dy, bottomRight.dx, bottomRight.dy, random);
    _addWobblyLine(path, bottomRight.dx, bottomRight.dy, bottomLeft.dx, bottomLeft.dy, random);
    _addWobblyLine(path, bottomLeft.dx, bottomLeft.dy, top.dx, top.dy, random);
    
    canvas.drawPath(path, paint);
  }
  
  // 绘制波浪
  void _drawWave(Canvas canvas, Offset center, double radius, Paint paint, _PseudoRandom random) {
    final path = Path();
    final width = radius * 2;
    final height = radius * 0.5;
    final waveCount = 3;
    
    path.moveTo(center.dx - width / 2, center.dy);
    
    for (var i = 0; i <= waveCount * 2; i++) {
      final t = i / (waveCount * 2);
      final x = center.dx - width / 2 + width * t;
      final y = center.dy + (i % 2 == 0 ? -1 : 1) * height + random.nextDouble() * 4 - 2;
      
      if (i == 0) {
        path.moveTo(x, y);
      } else {
        final controlX = center.dx - width / 2 + width * (i - 0.5) / (waveCount * 2);
        final controlY = center.dy + random.nextDouble() * 4 - 2;
        
        path.quadraticBezierTo(controlX, controlY, x, y);
      }
    }
    
    canvas.drawPath(path, paint);
  }
  
  // 绘制涂鸦
  void _drawScribble(Canvas canvas, Offset center, double radius, Paint paint, _PseudoRandom random) {
    final path = Path();
    final pointCount = 20;
    
    var lastX = center.dx + (random.nextDouble() * 2 - 1) * radius;
    var lastY = center.dy + (random.nextDouble() * 2 - 1) * radius;
    
    path.moveTo(lastX, lastY);
    
    for (var i = 0; i < pointCount; i++) {
      final x = center.dx + (random.nextDouble() * 2 - 1) * radius;
      final y = center.dy + (random.nextDouble() * 2 - 1) * radius;
      
      final controlX = (lastX + x) / 2 + random.nextDouble() * 20 - 10;
      final controlY = (lastY + y) / 2 + random.nextDouble() * 20 - 10;
      
      path.quadraticBezierTo(controlX, controlY, x, y);
      
      lastX = x;
      lastY = y;
    }
    
    canvas.drawPath(path, paint);
  }
  
  // 绘制波浪圆
  void _drawWobblyCircle(Canvas canvas, Offset center, double radius, Paint paint, _PseudoRandom random) {
    final path = Path();
    final segments = 20;
    
    for (var i = 0; i <= segments; i++) {
      final theta = i * 2 * math.pi / segments;
      final wobble = 1.0 + (random.nextDouble() * 0.1 - 0.05);
      final x = center.dx + radius * wobble * math.cos(theta);
      final y = center.dy + radius * wobble * math.sin(theta);
      
      if (i == 0) {
        path.moveTo(x, y);
      } else {
        path.lineTo(x, y);
      }
    }
    
    path.close();
    canvas.drawPath(path, paint);
  }
  
  // 添加波浪线
  void _addWobblyLine(Path path, double startX, double startY, double endX, double endY, _PseudoRandom random) {
    final dx = endX - startX;
    final dy = endY - startY;
    final distance = math.sqrt(dx * dx + dy * dy);
    final segments = (distance / 10).ceil();
    
    for (var i = 1; i <= segments; i++) {
      final t = i / segments;
      final x = startX + dx * t;
      final y = startY + dy * t;
      
      // 添加一些随机偏移使线条看起来不规则
      final offset = random.nextDouble() * 3 - 1.5;
      final perpX = -dy / distance * offset;
      final perpY = dx / distance * offset;
      
      path.lineTo(x + perpX, y + perpY);
    }
  }
  
  @override
  bool shouldRepaint(_HandDrawnDecorationPainter oldDelegate) {
    return oldDelegate.type != type ||
        oldDelegate.color != color;
  }
}

/// 伪随机数生成器，用于生成一致的"随机"效果
class _PseudoRandom {
  int _seed;
  
  _PseudoRandom(this._seed);
  
  double nextDouble() {
    _seed = (_seed * 1103515245 + 12345) & 0x7FFFFFFF;
    return _seed / 0x7FFFFFFF;
  }
  
  int nextInt(int max) {
    return (nextDouble() * max).floor();
  }
} 